/*********************************************************************
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2024, USTC
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of USTC nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *********************************************************************/

/* Author: Xinsheng Tang */
#include "pilz_tutorial/pilz_base.h"

// 分段轨迹规划
void replay_single_func(manipulator& ur5e) {
  // 利用PTP(点到点)规划方法,到达joint1位置
  ur5e.move_single("joint1",0.1,0.1,"PTP");
  // 利用LIN(直线)规划方法,到达joint2位置
  ur5e.move_single("joint2",0.1,0.1,"LIN");
  // 利用PTP(点到点)规划方法,到达joint3位置
  ur5e.move_single("joint3",0.1,0.1,"PTP");
}

// 带有过渡段的融合轨迹规划
void replay_sequence_func(manipulator& ur5e) {
  // 定义需要经过的路径点: 当前位置 --trj1-> joint1 --trj2-> joint2 --trj3-> joint3
  std::vector<std::string> V_name{"joint1","joint2","joint3"};
  // 定义每段轨迹的规划方法:trj1为PTP,trj2为直线,trj3为点到点
  std::vector<std::string> V_planner1{"PTP", "LIN", "PTP"};
  // 定义每段轨迹的运动速度的缩放系数(实际速度=缩放系数*最大速度):trj1为0.1,trj2为0.1,trj3为0.1
  std::vector<double> V_speed{0.1, 0.1, 0.1};
  // 定义每段轨迹的运动加速度的缩放系数(实际加速度=缩放系数*最大加速度):trj1为0.1,trj2为0.1,trj3为0.1
  std::vector<double> V_acc{0.1, 0.1, 0.1};
  // 定义每段轨迹的过渡段半径缩放系数:trj1为0.2,trj2为0.2,trj3为0
  std::vector<double> V_radius{0.2, 0.2, 0};
  // 进行带有过渡段的融合轨迹规划并执行
  ur5e.move_seq(V_name,V_radius,V_speed,V_acc,V_planner1);
}

int main(int argc, char **argv) {
  ros::init(argc, argv, "pilz_replay");
  setlocale(LC_ALL, "");
  ros::NodeHandle nh;
  moveit::planning_interface::MoveGroupInterfacePtr mgptr =
      std::make_shared<moveit::planning_interface::MoveGroupInterface>(
          "manipulator");
  manipulator ur5e(nh, mgptr);
  ros::AsyncSpinner spinner(10);
  spinner.start();
  if(argc != 2) {
    ROS_INFO_NAMED("pilz_tutorial","请输入参数: single 为分段轨迹规划, sequence 带有过渡段的融合轨迹规划");
  }
  if(strcmp(argv[1],"single") == 0) {
    ROS_INFO_NAMED("pilz_tutorial","正在进行:分段轨迹规划");
    replay_single_func(ur5e);
    ROS_INFO_NAMED("pilz_tutorial","执行完成!");
  } else if(strcmp(argv[1],"sequence") == 0) {
    ROS_INFO_NAMED("pilz_tutorial","正在进行:带有过渡段的融合轨迹规划");
    replay_sequence_func(ur5e);
    ROS_INFO_NAMED("pilz_tutorial","执行完成!");
  }
  ros::shutdown();
  return 0;
}
